import hex_md5 from './md5_32';
import axios from './axios'

// 屏幕分辨率
var _screen = window.screen.width + "*" + window.screen.height;

// 获取系统信息
function getSysInfo() {
  var ua = navigator.userAgent.toLowerCase(),
    isWin10 = ua.indexOf("nt 10.0") > -1,
    isWin7 = ua.indexOf("nt 6.1") > -1,
    isVista = ua.indexOf("nt 6.0") > -1,
    isWin2003 = ua.indexOf("nt 5.2") > -1,
    isWinXp = ua.indexOf("nt 5.1") > -1,
    isWin2000 = ua.indexOf("nt 5.0") > -1,
    isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),
    isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),
    isAir = (ua.indexOf("adobeair") != -1),
    isLinux = (ua.indexOf("linux") != -1);
  var broser = "";
  var sys = "";
  if (isWin10) {
    sys = "Windows 10";
  } else if (isWin7) {
    sys = "Windows 7";
  } else if (isVista) {
    sys = "Vista";
  } else if (isWinXp) {
    sys = "Windows xp";
  } else if (isWin2003) {
    sys = "Windows 2003";
  } else if (isWin2000) {
    sys = "Windows 2000";
  } else if (isWindows) {
    sys = "Windows";
  } else if (isMac) {
    sys = "Macintosh";
  } else if (isAir) {
    sys = "Adobeair";
  } else if (isLinux) {
    sys = "Linux";
  } else {
    sys = "Unknow";
  }
  return sys;
}

// 获取浏览器类型
function GetBrowserType() {
  var ua = navigator.userAgent.toLowerCase();
  if (ua == null) {
    return "ie";
  } else if (ua.indexOf('chrome') != -1) {
    return "chrome";
  } else if (ua.indexOf('opera') != -1) {
    return "opera";
  } else if (ua.indexOf('msie') != -1) {
    return "ie";
  } else if (ua.indexOf('safari') != -1) {
    return "safari";
  } else if (ua.indexOf('firefox') != -1) {
    return "firefox";
  } else if (ua.indexOf('gecko') != -1) {
    return "gecko";
  } else {
    return "ie";
  }
}

// 获取浏览器版本
function GetBrowserVersion() {

  var ua = navigator.userAgent.toLowerCase();

  if (ua == null) return "null";

  else if (ua.indexOf('chrome') != -1) return ua.substring(ua.indexOf('chrome') + 7, ua.length).split(' ')[0];

  else if (ua.indexOf('opera') != -1) return ua.substring(ua.indexOf('version') + 8, ua.length);

  else if (ua.indexOf('msie') != -1) return ua.substring(ua.indexOf('msie') + 5, ua.length - 1).split(';')[0];

  else if (ua.indexOf('safari') != -1) return ua.substring(ua.indexOf('safari') + 7, ua.length);

  else if (ua.indexOf('gecko') != -1) return ua.substring(ua.indexOf('firefox') + 8, ua.length);

  else return "null";
}

var visit_system = getSysInfo();
var visit_browser = GetBrowserType();
var visit_browser_version = GetBrowserVersion();
var visit_resolution = _screen;
var visit_user;
var visit_user_sfzhm;
var visit_session_id;

function insertData(data) {
  axios({
    method: "GET",
    url: "/tongji/insertData",
    params: data,
    dataType: "application/json; charset=utf-8",
    async: true,
  }).then((res) => {
    console.log('res', res)
  });
}

function viewNum(visit_service) {
  axios({
    method: "GET",
    url: "/dmbgl/getSesId"
  }).then(function (res) {
    sessionStorage.setItem('visit_session_id', res.returnData.visit_session_id);

    var text = visit_system + visit_browser + visit_browser_version + visit_resolution + visit_service;
    var md5 = hex_md5(text);
    var data = {
      "visit_system": visit_system,
      "visit_browser": visit_browser,
      "visit_browser_version": visit_browser_version,
      "visit_resolution": visit_resolution,
      "visit_user": visit_user,
      "visit_user_sfzhm": visit_user_sfzhm,
      "visit_service": visit_service,
      "visit_session_id": sessionStorage.getItem('visit_session_id'),
      "md5": md5
    }
    insertData(data);
  });
}
export default viewNum
